package com.itcncp.ipaybox.server.server;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author LiuPeng
 * 
 */
public class NettyChannelMap {
	
	private static Log logger = LogFactory.getLog(NettyChannelMap.class);
	
	private static Map<String, SocketChannel> map = new ConcurrentHashMap<String, SocketChannel>();

	public static void add(String clientId, SocketChannel socketChannel) {
		map.put(clientId, socketChannel);
	}

	public static Channel get(String clientId) {
		return map.get(clientId);
	}

	public static void remove(SocketChannel socketChannel) {
		for (Map.Entry<String, SocketChannel> entry : map.entrySet()) {
			if (entry.getValue() == socketChannel) {
				map.remove(entry.getKey());
				logger.info("client:"+entry.getKey()+",下线");
			}
		}
	}

	public static Map<String, SocketChannel> getMap() {
		return map;
	}

	public static void setMap(Map<String, SocketChannel> map) {
		NettyChannelMap.map = map;
	}

}
